<?php
namespace app\common\lib\db;
use think\Controller;
use think\Db;

class DbCommon extends Controller
{
    public static function get_media_msg($id,$type)
    {
        if($type == '1') {
            $sql = "select * from up_video,up_video_modular,up_video_type,up_person 
	                    where up_video.video_f_id=up_video_modular.video_modular_id
		                    and up_video.f_id=up_video_type.video_type_id
		                        and up_video.person_id = up_person.person_id
				                    and up_video.video_id = $id;";
            $ret = Db::query($sql);
            return $ret;
        }elseif ($type == '2') {
            $sql = "select * from up_music,up_music_modular,up_music_type ,up_person
	                    where up_music.music_f_id=up_music_modular.video_modular_id
		                    and up_music.f_id=up_music_type.music_type_id
		                        and up_music.person_id = up_person.person_id
				                    and up_music.music_id = $id;";
            $ret = Db::query($sql);
            return $ret;
        }else {
            return '';
        }
    }

    /**
     * 指定字段查询数据
     * @param $name
     * @param $arr
     * @return array $data
     */
    public static function only($name, $arr)
    {
        $item = [];
        foreach ($name as $key) {
            if (isset($param[$key])) {
                $item[$key] = $arr[$key];
            }
        }
        return $item;
    }

    /**
     * @param number $district_id 地区id
     * @return string $ret
     */
    public static function get_area($district_id)
    {
        if(!isset($district_id) || empty($district_id) || !is_numeric($district_id)) {
            return '';
        }
        $sql = "SELECT district FROM up_area WHERE district_id = $district_id;";
        $ret = Db::query($sql);
        return $ret;
    }

    public static function area($sheng,$shi,$qu)
    {
        $a_sheng = self::get_area($sheng);
        $a_shi   = self::get_area($shi);
        $a_qu    = self::get_area($qu);
        if(!$a_sheng) {
            return '';
        }elseif (!$a_shi) {
            return $a_sheng['0']['district'];
        }elseif (!$a_qu) {
            return $a_sheng['0']['district'] . $a_shi['0']['district'];
        }else {
            return $a_sheng['0']['district'] . $a_shi['0']['district'] . $a_qu['0']['district'];
        }
    }

    /**
     * @return array $data 问题反馈首页
     * @return number $data['id'] 列表id
     * @return string $data['name'] 一级标题
     * @return string $data['title'] 二级标题
     */
    public static function question_list()
    {
        $sql = "SELECT up_question.id,up_question_type.`name`,up_question.title
	                FROM up_question,up_question_type
		                WHERE up_question.type = up_question_type.type_id";
        $ret = Db::query($sql);
        return $ret;
    }

    /**
     * @return array $data
     * 剧团介绍列表展示，默认4类
     */
    public static function get_troupe_list()
    {
        $sql = "SELECT troupe_id,troupe_title,troupe_img FROM up_troupe;";
        $ret = Db::query($sql);
        return $ret;
    }

    public static function get_media_url($id,$is_type)
    {
        if($is_type == '1') {
            $sql = "SELECT * FROM up_video_chirden WHERE chirden_id = $id;";
        }elseif ($is_type == '2') {
            $sql = "SELECT * FROM up_music_chirden WHERE chirden_id = $id;";
        }else {
            exception('请求参数错误');
        }
        $ret = Db::query($sql);
        return $ret;
    }

    /**
     * @param int $video_id up_video.video_id
     * @param int $children_id up_video_chirden.f_id
     * @return array $ret;
     */
    public static function get_video_children($video_id,$children_id)
    {
        $sql = "SELECT * FROM up_video,up_video_chirden WHERE up_video_chirden.chirden_id=$children_id and up_video.video_id=$video_id;";
        $ret = Db::query($sql);
        return $ret;
    }
}